//
//  MyBidJobDetailTabDetailSectionOffer.swift
//  GreenDeal
//
//  Created by Moennig Q on 2023/4/18.
//

import UIKit
import SnapKit
import RxCocoa
import RxSwift
class ServiceDetailTabDetailSectionMyOffer: CustomBackView {
    private lazy var disposeBag = DisposeBag()
    
    let offersCntLabel = BaseLabel()
    let tableView = TableView()
    var tableViewHeightConstant: Constraint?
    
    override func setUpView() {
        let imageView = UIImageView(image: R.image.service_detail_myoffer_section_icon())
        self.addSubview(imageView)
        imageView.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(80)
        }
        //
        tableView.backgroundColor = .clear
        self.addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.top.equalTo(imageView.snp.bottom)
            make.left.right.equalToSuperview()
            self.tableViewHeightConstant = make.height.greaterThanOrEqualTo(0).constraint
            make.bottom.equalTo(-16)
        }
        tableView.register(ServiceDetailOfferTableViewCell.self, forCellReuseIdentifier: ServiceDetailOfferTableViewCell.reuseCellIdentifier)
        //监听高度变化
        tableView.rx.observe(CGSize.self,"contentSize").subscribe {[weak self] (size) in
            guard let contentSize = size else {return}
            self?.tableViewHeightConstant?.update(offset: contentSize.height)
        }.disposed(by: self.disposeBag)
        //绑定offer数据源
        
        ServiceDetailModelHelper.shared.offers.bind(to: tableView.rx.items(cellIdentifier: ServiceDetailOfferTableViewCell.reuseCellIdentifier,cellType: ServiceDetailOfferTableViewCell.self)){(row, model, cell) in
            cell.setData(model)
        }.disposed(by: self.disposeBag)
//        //
//        let offerModel = ServiceDetailOfferModel()
//        offerModel.created_at = "7 minutes ago"
//        offerModel.installer_name = "Jimmy Xu"
//        offerModel.price = 300
//        offerModel.message = "1.下载中的任务，用url作为key添加到DownloadManager单例中;2.取消任务、删除任务以及下载失败时，将下载任务从单例中删除，保证- DownloadManager单例的downloadTasks中是正在下载的3.暂停下载，保存已经下载的data到plist文件   DaisyNet.download(url).downloadProgress { progress in"
//        var list: [ServiceDetailOfferModel] = [ServiceDetailOfferModel]()
//        list.append(offerModel)
//        ServiceDetailModelHelper.shared.offers.onNext(list)
    }

}
